import { apis, server, userInfo } from '../config'
import { getGuid } from '../utils/getDefaultValue'
import request from '../utils/request'

export function queryMaterials (page = 1, pageSize = 10, searchedKey = 'oea01', searchedValue = '') {
  const urlSearchParams = new URLSearchParams()
  urlSearchParams.set('report_id', 929)
  urlSearchParams.set('limit', pageSize)
  urlSearchParams.set('start', (page - 1) * pageSize)
  urlSearchParams.set('query_param', JSON.stringify([
    {
      'param_name': searchedKey,
      'operator': '包含',
      'value': searchedValue
    }
  ]))

  return request({
    url: server + apis.getReportData,
    method: 'POST',
    data: urlSearchParams.toString()
  })
}

export function queryBills (formData, page = 1, pageSize = 100000) {
  const urlSearchParams = new URLSearchParams()
  urlSearchParams.set('report_id', 918)
  urlSearchParams.set('limit', pageSize)
  urlSearchParams.set('start', (page - 1) * pageSize)
  const params = []
  if (formData.range.length >= 2) {
    params.push({
      'param_name': 'ole',
      'operator': '=',
      'value': formData.range[1] + '->' + formData.range[0]
    })
  }
  if (formData.cus) {
    params.push({
      'param_name': 'cus',
      'operator': '包含',
      'value': formData.cus
    })
  }
  if (formData.itm) {
    params.push({
        'param_name': 'itm',
        'operator': '包含',
        'value': formData.itm
      }
    )
  }
  if (formData.customer_name) {
    params.push({
      'param_name': 'customer_name',
      'operator': '包含',
      'value': formData.customer_name
    })
  }
  if (formData.itm_name) {
    params.push({
        'param_name': 'itm_name',
        'operator': '包含',
        'value': formData.itm_name
      }
    )
  }
  if (formData.oeb01) {
    params.push({
        'param_name': 'oeb01',
        'operator': '包含',
        'value': formData.oeb01
      }
    )
  }
  if (params.length > 0) {
    urlSearchParams.set('query_param', JSON.stringify(params))
  }

  return request({
    url: server + apis.getReportData,
    method: 'POST',
    data: urlSearchParams.toString()
  })
}

export function queryBillSettings () {
  const urlSearchParams = new URLSearchParams()
  urlSearchParams.set('report_id', 918)

  return request({
    url: server + apis.getGridJs,
    method: 'POST',
    data: urlSearchParams.toString()
  })
}

export function deleteBills (deleteDatas) {
  const urlSearchParams = new URLSearchParams()
  urlSearchParams.set('report_id', 918)
  urlSearchParams.set('pk_rows', JSON.stringify(deleteDatas))
  return request({
    url: server + apis.delBill,
    method: 'POST',
    data: urlSearchParams.toString()
  })
}

export function updateBills (oldDatas, newDatas) {
  const urlSearchParams = new URLSearchParams()
  urlSearchParams.set('report_id', 918)
  urlSearchParams.set('old_full_rows', JSON.stringify(oldDatas))
  urlSearchParams.set('new_full_rows', JSON.stringify(newDatas))

  return request({
    url: server + apis.editSingleDynReportData,
    method: 'POST',
    data: urlSearchParams.toString()
  })
}

export function updateBill (oldData, newData) {
  const urlSearchParams = new URLSearchParams()
  urlSearchParams.set('report_id', 918)
  urlSearchParams.set('old_full_rows', JSON.stringify([oldData]))
  urlSearchParams.set('new_full_rows', JSON.stringify([newData]))

  return request({
    url: server + apis.editSingleDynReportData,
    method: 'POST',
    data: urlSearchParams.toString()
  })
}

export function addBill (newData) {
  var formData = newBillFormData({
    oid: getGuid(),
    cus: newData.customer,
    itm: newData.material,
    ole: newData.date,
    qty: newData.quantity
  })

  const urlSearchParams = new URLSearchParams()
  urlSearchParams.set('report_id', 918)
  urlSearchParams.set('old_full_rows', JSON.stringify([{}]))
  urlSearchParams.set('new_full_rows', JSON.stringify([formData]))

  return request({
    url: server + apis.editSingleDynReportData,
    method: 'POST',
    data: urlSearchParams.toString()
  })
}

var originData = {
  'oid': 'e7aa60d0d30c4d3a83a9eb3f449bd349',
  'cus': 'A10003',
  'itm': 'BA100037800',
  'name': null,
  'ind': 0,
  'ole': '2021-01-16',
  'ols': null,
  'qty': '1000',
  'qy2': 99,
  'eid': null,
  'ena': 0,
  'dir': 0,
  'ced': null,
  'ces': null,
  'oed': null,
  'oes': '2021-01-15',
  'pln': null,
  'pri': 0,
  'eff': 1,
  'cls': 0,
  'rid': 0,
  'npl': 0,
  'dps': null,
  'acd': null,
  'scd': null,
  'clr': -6750004,
  'cmm': 'SpreadSheet测试',
  'ucr': userInfo.org_code || 'CD7411',
  'cre': '2021-08-18',
  'umd': userInfo.org_code || 'CD7411',
  'mdf': '2021-08-19',
  'code': '202108180000126'
}

export function newBillFormData (params, origin = originData) {
  if (params === null) {
    return origin
  }

  return {
    ...origin,
    ...params
  }
}

export function queryOrderList (cellKey, item) {
  const urlSearchParams = new URLSearchParams()
  urlSearchParams.set('report_id', 918)
  urlSearchParams.set('row_data', JSON.stringify({
    cus: item.customer,
    itm: item.material
  }))

  urlSearchParams.set('col', 'total_qty')

  return request({
    url: server + apis.getColTipData,
    method: 'POST',
    data: urlSearchParams.toString()
  })
}
